import { URL_attachment } from '@/api/index'
import Vue from 'vue'

// 防抖函数 触发高频事件后n秒内函数只会执行一次
export const Debounce = (fn, t) => {
  let delay = t || 500
  let timer
  return function () {
    let args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      timer = null
      fn.apply(this, args)
    }, delay)
  }
}

// 节流：高频事件触发，但在n秒内只会执行一次
export const Throttle = (fn, t) => {
  let last
  let timer
  let interval = t || 500
  return function () {
    let args = arguments
    let now = +new Date()
    if (last && now - last < interval) {
      clearTimeout(timer)
      timer = setTimeout(() => {
        last = now
        fn.apply(this, args)
      }, interval)
    } else {
      last = now
      fn.apply(this, args)
    }
  }
}

// 不同分辨率下像素转换
export const pxTransform = (num, type) => {
  let finalnum = 0
  let docEl = window.document.documentElement.style.fontSize
  let currentFont = parseInt(docEl.replace('px', ''))
  if (type == 'int') {
    finalnum = Math.round((currentFont * num) / 264.6)
    // finalnum = Math.round((currentFont*num)/192)
  } else {
    finalnum = parseFloat(((currentFont * num) / 264.6).toFixed(2))
    // finalnum = parseFloat(((currentFont*num)/192).toFixed(2))
  }
  return finalnum
}

export const arrDistinctByProp = (arr, prop) => {
  let obj = {}
  return arr.reduce(function (preValue, item) {
    obj[item[prop]] ? '' : (obj[item[prop]] = true && preValue.push(item))
    return preValue
  }, [])
}

export const logRecord = (module, content, thisObj) => {
  let url = gl.serverURL + '/user/whLog'
  let params = {
    module: module,
    content: content,
  }
  thisObj.$http.post(url, params).then((res) => {})
}

export const cloneDeep = function (obj) {
  const cache = new WeakMap()

  const _cd = (obj) => {
    if (typeof obj !== 'object' || obj === null) return obj
    if (cache.has(obj)) return cache.get(obj)

    const result = Array.isArray(obj) ? [] : {}
    cache.set(obj, result)

    for (let k in obj) {
      if (obj.hasOwnProperty(k)) {
        result[k] = cloneDeep(obj[k])
      }
    }

    return result
  }
  return _cd(obj)
}

export const objectToFormData = (obj) => {
  const formData = new FormData()
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      formData.append(key, obj[key])
    }
  }
  return formData
}

export const objValue = (obj, str) => {
  if (typeof obj !== 'object' || obj === null) return ''
  let v = obj
  const arr = str.split('.')
  for (let i = 0, len = arr.length; i < len; i++) {
    v = v[arr[i]]
    if (!v) return v
  }
  return v
}

export const attachmenDownload = (data) => {
  const a = document.createElement('a')
  a.href = URL_attachment + data
  a.download = ''
  document.body.appendChild(a)
  a.click()
  setTimeout(document.body.removeChild, 10, a)
}

export const handleRequestStatus = (res, tipText, cb) => {
  const status = res.data.status
  if (status === 0) {
    tipText && Vue.prototype.$Message.success(res.data.message || tipText + '成功')
    if (typeof cb === 'function') {
      cb()
    }
    return true
  } else {
    tipText && Vue.prototype.$Message.error(res.data.message || tipText + '失败')
    return false
  }
}
